package org.loofer.uikit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View.MeasureSpec;
import android.widget.GridView;

public class ExtendGridView extends GridView {
    private int mHorizontalSpacing;
    private int mNumColumns;
    private boolean mPenetrateTouch;
    private boolean mStretchable;
    private int mVerticalSpacing;

    public ExtendGridView(Context context) {
        this(context, null);
    }

    public ExtendGridView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExtendGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExtendGridView);
        this.mStretchable = a.getBoolean(R.styleable.ExtendGridView_stretchable, false);
        this.mPenetrateTouch = a.getBoolean(R.styleable.ExtendGridView_penetrateTouch, false);
        a.recycle();
    }

    public void setHorizontalSpacing(int horizontalSpacing) {
        super.setHorizontalSpacing(horizontalSpacing);
        this.mHorizontalSpacing = horizontalSpacing;
    }

    public int getHorizontalSpacing() {
        return this.mHorizontalSpacing;
    }

    public void setVerticalSpacing(int verticalSpacing) {
        super.setVerticalSpacing(verticalSpacing);
        this.mVerticalSpacing = verticalSpacing;
    }

    public int getVerticalSpacing() {
        return this.mVerticalSpacing;
    }

    public void setNumColumns(int numColumns) {
        super.setNumColumns(numColumns);
        this.mNumColumns = numColumns;
    }

    @SuppressLint({"NewApi"})
    public int getNumColumns() {
        return this.mNumColumns;
    }

    public void setStretchable(boolean stretchable) {
        if (this.mStretchable != stretchable) {
            this.mStretchable = stretchable;
            requestLayout();
        }
    }

    public boolean isStretchable() {
        return this.mStretchable;
    }

    public void setPenetrateTouch(boolean penetrate) {
        this.mPenetrateTouch = penetrate;
    }

    public boolean isPenetrateTouch() {
        return this.mPenetrateTouch;
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (this.mStretchable) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(536870911, Integer.MIN_VALUE);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @SuppressLint({"NewApi"})
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    public boolean onTouchEvent(MotionEvent ev) {
        int motionPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        if (!this.mPenetrateTouch || motionPosition >= 0) {
            return super.onTouchEvent(ev);
        }
        return false;
    }
}
